//
//  PhotoViewCell.swift
//  CXCWeibo
//
//  Created by cxc on 15/7/5.
//  Copyright © 2015年 china. All rights reserved.
//

import UIKit
import SDWebImage
import FFAutoLayout

protocol PhotoViewCellDelegate:NSObjectProtocol{
     func photoViewCellDidTapImage()
     func photoViewerDidZooming(scale:CGFloat)
     func photoViewerDidEndZoom()
}

class PhotoViewCell: UICollectionViewCell,UIScrollViewDelegate{
    weak var photoDelegate:PhotoViewCellDelegate?
    
    var imageURL:NSURL?{
        didSet{
            resetScrollView()
            indicator.startAnimating()
            imageView.sd_setImageWithURL(imageURL!, placeholderImage: nil){
                (image,error,_,_) in
                self.indicator.stopAnimating()
                self.imagePostion(image)
            }
        }
    }
   
    private func resetScrollView(){
        scrollView.contentInset = UIEdgeInsetsZero
        scrollView.contentSize = CGSizeZero
        scrollView.contentOffset = CGPointZero
    }
    // 返回缩放的View
    func viewForZoomingInScrollView(scrollView: UIScrollView) -> UIView? {
        return imageView
    }
    private func imagePostion(image:UIImage){
        let size = displaySize(image)
        if bounds.height < size.height {
            imageView.frame = CGRect(origin: CGPointZero, size: size)
            scrollView.contentSize = size
        }else{
            let y = (bounds.height - size.height) * 0.5
            imageView.frame = CGRect(origin: CGPointZero, size: size)
            scrollView.contentInset = UIEdgeInsets(top: y, left: 0, bottom: 0, right: 0)
        }
    }
    func scrollViewDidEndZooming(scrollView: UIScrollView, withView view: UIView?, atScale scale: CGFloat) {
        var offsetX = (scrollView.bounds.width - view!.frame.width) * 0.5
        if offsetX < 0 {
            offsetX = 0
        }
        var offsetY = (scrollView.bounds.height - view!.frame.height) * 0.5
        if offsetY < 0 {
            offsetY = 0
        }
        scrollView.contentInset = UIEdgeInsets(top: offsetY, left: offsetX, bottom: 0, right: 0)
        photoDelegate?.photoViewerDidEndZoom()
    }
    
    func scrollViewDidZoom(scrollView: UIScrollView) {
        let scale = imageView.transform.a
        photoDelegate?.photoViewerDidZooming(scale)
    }
    /**
    根据图像计算现实的尺寸
    */
    private func displaySize(image:UIImage) -> CGSize{
        let scale = image.size.height / image.size.width
        let h = bounds.width * scale
        return CGSize(width: bounds.width, height: h)
    }
    func clickImage(){
        photoDelegate?.photoViewCellDidTapImage()
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        scrollView.frame = UIScreen.mainScreen().bounds
        addSubview(scrollView)
        scrollView.backgroundColor = UIColor.clearColor()
        
        scrollView.delegate = self
        scrollView.minimumZoomScale = 0.5
        scrollView.maximumZoomScale = 2.0
        
        scrollView.addSubview(imageView)
        
        let tap = UITapGestureRecognizer(target: self, action: "clickImage")
        imageView.addGestureRecognizer(tap)
        imageView.userInteractionEnabled = true
        
        addSubview(indicator)
        indicator.ff_AlignInner(ff_AlignType.CenterCenter, referView: self, size: nil)
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 懒加载
    lazy var imageView = UIImageView()
    lazy private var scrollView = UIScrollView()
    private lazy var indicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
}
